package com.comdo.newup_loan.bank.bank;

import android.annotation.SuppressLint;
import android.app.Activity;

import com.comdo.newup_loan.bank.BankUtils;
import com.comdo.newup_loan.comom.Config;

import java.security.GeneralSecurityException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import okhttp3.FormBody;


@SuppressLint("SimpleDateFormat")
public class BankApiManager {
    @SuppressLint("SimpleDateFormat")
    // 流水精确到毫秒
    private static final SimpleDateFormat format = new SimpleDateFormat(
            "yyyyMMddHHmmssSSS");
    private static final SimpleDateFormat sdf = new SimpleDateFormat(
            "yyyyMMddHHmmss");

    private BankApiManager() {
    }

    private static volatile BankApiManager instance;
    private static HashMap<String, Object> map;

    public static BankApiManager getIstance() {
        Date date = new Date();
        String requestNo = format.format(date);
        String timestamp = sdf.format(date);
        map = new HashMap<String, Object>();
        map.put("requestNo", requestNo);
        map.put("redirectUrl", BankConfig.redirectUrl);
        map.put("timestamp", timestamp);
        if (instance == null) {
            synchronized (BankApiManager.class) {
                if (instance == null) {
                    instance = new BankApiManager();
                }
            }
        }
        return instance;
    }
    /*
     * 2.1. 个人绑卡注册
     */
        public FormBody personalTieCardRegister(Activity tag,String platformUserNo) {
        int userRole = Config.loginInfo.getUserRole();
        String authListStr = "";
        map.put("platformUserNo", platformUserNo);
        //map.put("idCardType", BankConfig.PRC_ID);
        if (userRole == 0) {
            authListStr = BankConfig.CREDIT_ASSIGNMENT + ","
                    + BankConfig.TENDER;
            map.put("userRole", BankConfig.INVESTOR);
            map.put("authList", authListStr);
        } else {
            authListStr = BankConfig.REPAYMENT + "," + BankConfig.WITHDRAW
                    + "," + BankConfig.RECHARGE;
            map.put("userRole", BankConfig.BORROWERS);
            map.put("authList", authListStr);
        }
        map.put("checkType", "LIMIT");
        map.put("redirectUrl", BankConfig.redirectUrl1);
        map.put("userLimitType", "ID_CARD_NO_UNIQUE");
        Map<String, String> result = null;
        try {
            result = BankUtils.createPostParam(API.PERSONAL_REGISTER_EXPAND
                    + "", map, (Activity) tag);
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        }

            FormBody.Builder builder = new FormBody.Builder();
            for (String key : result.keySet()) {
                builder.add(key, result.get(key) + "");
            }
            FormBody body = builder.build();

            return body;
    }
    /*
	 * 2.10.会员激活
	 */
    public FormBody activityUser(Object tag, String platformUserNo) {
        int userRole = Config.loginInfo.getUserRole();
        String authListStr = "";
        map.put("platformUserNo", platformUserNo);
        if (userRole == 0) {
            authListStr = BankConfig.CREDIT_ASSIGNMENT + ","
                    + BankConfig.TENDER;
            map.put("authList", authListStr);
        } else {
            authListStr = BankConfig.REPAYMENT + "," + BankConfig.WITHDRAW
                    + "," + BankConfig.RECHARGE;
            map.put("authList", authListStr);
        }
        map.put("checkType", "LIMIT");// N
        Map<String, String> result = null;
        try {
            result = BankUtils.createPostParam(API.ACTIVATE_STOCKED_USER + "",
                    map, (Activity) tag);

        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        }
        FormBody.Builder builder = new FormBody.Builder();
        for (String key : result.keySet()) {
            builder.add(key, result.get(key) + "");
        }
        FormBody body = builder.build();

        return body;
    }
    /*
         * 3.1. 充值(移动端仅支持快捷支付)
         */
    @SuppressLint("SimpleDateFormat")
    public FormBody recharge(Activity tag, String requestNo, String amount, String payType) {
        String bankcode = BankCode.getBankCode(Config.userInfo.getBankName());
        map.put("requestNo", requestNo);
        map.put("platformUserNo", Config.userInfo.getPlatformUserNo());// Y
        map.put("amount", amount);// Y
        map.put("expectPayCompany", payType);
        map.put("rechargeWay", BankConfig.SWIFT);
        map.put("bankcode", bankcode);
        Date date;
        Map<String, String> result = null;
        try {
            date = format.parse(requestNo);
            map.put("timestamp", sdf.format(date));
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.add(Calendar.MINUTE, 5);
            map.put("expired", sdf.format(c.getTime()));// Y
            result = BankUtils.createPostParam(API.RECHARGE + "", map,
                    (Activity) tag);

        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        FormBody.Builder builder = new FormBody.Builder();
        for (String key : result.keySet()) {
            builder.add(key, result.get(key) + "");
        }
        FormBody body = builder.build();

        return body;
    }

    /*
         * 3.3. 提现
         */
    public FormBody withdrawals(Activity tag,
                                String requestNo, double amount,
                                double commission) {
        map.put("platformUserNo", Config.userInfo.getPlatformUserNo());
        map.put("requestNo", requestNo);
        map.put("amount", amount);
        if (commission != 0) {
            map.put("commission", commission);
            map.put("withdrawType", "NORMAL");
            map.put("withdrawForm", "CONFIRMED");
        } else {
            map.put("withdrawType", "URGENT");
            map.put("withdrawForm", "IMMEDIATE");
        }
        Date date;
        Map<String, String> result = null;
        try {
            date = format.parse(requestNo);
            map.put("timestamp", sdf.format(date));
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.add(Calendar.MINUTE, 5);
            map.put("expired", sdf.format(c.getTime()));// Y
            result = BankUtils.createPostParam(API.WITHDRAW + "", map,
                    (Activity) tag);

        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        } catch (ParseException e) {

            e.printStackTrace();
        }
        FormBody.Builder builder = new FormBody.Builder();
        for (String key : result.keySet()) {
            builder.add(key, result.get(key) + "");
        }
        FormBody body = builder.build();

        return body;

    }

    /*
         * 3.3. 验密扣费(交违约金)
         */
    public FormBody payPenaltyAmount(Activity tag, String requestNo, double amount, String targetPlatformUserNo) {
        map.put("platformUserNo", Config.userInfo.getPlatformUserNo());
        map.put("requestNo", requestNo);
        map.put("amount", amount);
        map.put("customDefine", "缴纳违约金");
        map.put("targetPlatformUserNo", targetPlatformUserNo);
        Date date;
        Map<String, String> result = null;
        try {
            date = format.parse(requestNo);
            map.put("timestamp", sdf.format(date));
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.add(Calendar.MINUTE, 5);
            map.put("expired", sdf.format(c.getTime()));// Y
            result = BankUtils.createPostParam(API.VERIFY_DEDUCT + "", map,
                    (Activity) tag);
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        FormBody.Builder builder = new FormBody.Builder();
        for (String key : result.keySet()) {
            builder.add(key, result.get(key) + "");
        }
        FormBody body = builder.build();

        return body;
    }

    /*
         * 2.3. 个人换绑卡
         */
    public FormBody replaceCard(Activity tag, String requestNo) {
        map.put("platformUserNo", Config.userInfo.getPlatformUserNo());
        map.put("requestNo", requestNo);
        map.put("checkType", "LIMIT");
        map.put("bindType", "UPDATE_BANKCARD");

        Map<String, String> result = null;
        try {
            result = BankUtils.createPostParam(API.PERSONAL_BIND_BANKCARD_EXPAND + "", map,
                    (Activity) tag);
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        }
        FormBody.Builder builder = new FormBody.Builder();
        for (String key : result.keySet()) {
            builder.add(key, result.get(key) + "");
        }
        FormBody body = builder.build();

        return body;
    }

    /*
     * 接口后缀名
     */
    public static enum API {
        // 2.1. 个人绑卡注册
        PERSONAL_REGISTER_EXPAND,
        //2.3. 个人换绑卡
        PERSONAL_BIND_BANKCARD_EXPAND,
        // 2.5. 解绑银行卡
        UNBIND_BANKCARD,
        // 2.10会员激活
        ACTIVATE_STOCKED_USER,
        // 3.1. 充值
        RECHARGE,
        // 3.3提现
        WITHDRAW,
        //4.12. 验密扣费
        VERIFY_DEDUCT,
    }
}
